package com.example.service;

import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Category;
import com.example.mapper.CategoryMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: 康伟奇
 * @date: 2023 - 11 - 11 00:28
 **/
@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BusinessService businessService;

    //检查是否是商家登录后台系统，如果是则根据商家id查询所有分类
    private void checkIsBusiness(Category category){
        //获取到当前登录的用户成员信息
        Account currentUser = TokenUtils.getCurrentUser();
        String role = currentUser.getRole();
        //如果是商家只能查出自己的分类
        if (RoleEnum.BUSINESS.name().equals(role)){
            //设置商家自己的Id作为查询条件
            category.setBusinessId(currentUser.getId());
        }
    }

    /**
     * 新增
     * @param category
     */
    public void add(Category category) {
        //校验新增权限
        businessService.checkBusinessAuth();
        //获取当前的用户信息，如果角色等于商家则设置category里面的businessId为当前登录用户（商家）的Id
        Account currentUser = TokenUtils.getCurrentUser();
        if (RoleEnum.BUSINESS.name().equals(currentUser.getRole())){
            category.setBusinessId(currentUser.getId());
        }
        categoryMapper.insert(category);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(Integer id) {
        businessService.checkBusinessAuth();
        categoryMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        businessService.checkBusinessAuth();
        for (Integer id : ids) {
            categoryMapper.deleteById(id);
        }
    }


    /**
     * 查询所有
     * @param category
     * @return
     */
    public List<Category> selectAll(Category category) {
        checkIsBusiness(category);
       return categoryMapper.selectAll(category);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Category selectById(Integer id) {
        businessService.checkBusinessAuth();
        return categoryMapper.selectById(id);
    }

    /**
     * 分页查询
     * @param category
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<Category> selectPage(Category category, Integer pageNum, Integer pageSize) {
        checkIsBusiness(category);
        PageHelper.startPage(pageNum,pageSize);
        List<Category> list = categoryMapper.selectAll(category);
        return PageInfo.of(list);
    }

    /**
     * 更新
     * @param category
     */
    public void updateById(Category category) {
        businessService.checkBusinessAuth();
        categoryMapper.updateById(category);
    }

    //根据商家id进行删除
    public void deleteByBusinessId(Integer businessId) {
        categoryMapper.deleteByBusinessId(businessId);
    }
}
